package com.jieao.template.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jieao.template.common.util.CurrentlyLoggedInUtil;
import com.jieao.template.common.util.JsonUtils;
import com.jieao.template.entity.permissionMgr.PermissionDO;
import com.jieao.template.entity.permissionMgr.PermissionTreeVO;
import com.jieao.template.entity.pub.Pagination;
import com.jieao.template.mapper.PermissionMapper;
import com.jieao.template.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限表(Permission)表服务实现类
 *
 * @author jzw
 * @since 2020-04-30 09:31:06
 */
@Service
public class PermissionServiceImpl implements PermissionService
{
    @Resource
    private PermissionMapper permissionMapper;

    /**
     * 获取当前登录用户的权限code
     *
     * @return java.util.List<java.lang.String>
     * @author Jzw
     * @date 2019/12/27 15:40
     */
    @Override
    public List<String> getAllPermissionByUserId()
    {
        return permissionMapper.getAllPermissionByUserId(CurrentlyLoggedInUtil.getUserId());
    }

    /**
     * 权限树
     *
     * @return java.lang.Object
     * @author Jzw
     * @date 2019/12/17 13:15
     */
    @Override
    public List<PermissionTreeVO> getPermissionTree()
    {
        return permissionMapper.getPermissionTree();
    }

    /**
     * 停启用权限
     *
     * @param id 权限id
     * @return java.lang.Object
     * @author Jzw
     * @date 2019/12/16 15:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMenuOrPermission(String id)
    {
        permissionMapper.delMenuOrPermission(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Override
    public PermissionDO queryById(String id)
    {
        return permissionMapper.queryById(id);
    }

    /**
     * 查询分页数据
     *
     * @param pagination 分页对戏
     * @return PageInfo 对象
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Override
    public PageInfo<PermissionDO> queryAllByLimit(Pagination pagination)
    {
        PageHelper.startPage(pagination.getPageNum(),
                pagination.getPageSize());
        return new PageInfo<>(permissionMapper.queryAllByLimit(JsonUtils.map2pojo(pagination.getParams(), PermissionDO.class)));
    }

    /**
     * 查询全部数据
     *
     * @param permission 筛选对象
     * @return 列表数据
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Override
    public List<PermissionDO> queryAll(PermissionDO permission)
    {
        return permissionMapper.queryAll(permission);
    }

    /**
     * 新增数据
     *
     * @param permission 实例对象
     * @return 实例对象
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PermissionDO insert(PermissionDO permission)
    {
        permission.setCreateId(CurrentlyLoggedInUtil.getUserId());
        permission.setEnable(true);
        permissionMapper.insert(permission);
        return permission;
    }

    /**
     * 修改数据
     *
     * @param permission 实例对象
     * @return 实例对象
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(PermissionDO permission)
    {
        permission.setUpdateId(CurrentlyLoggedInUtil.getUserId());
        return permissionMapper.update(permission);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(String id)
    {
        return permissionMapper.deleteById(id) > 0;
    }

    /**
     * 通过主键集合批量删除数据
     *
     * @param idList 主键集合
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeletionByIdList(List<String> idList)
    {
        permissionMapper.batchDeletionByIdList(idList);
    }
}